package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Accident;
import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.mapper.AccidentMapper;
import cn.wolfcode.crm.mapper.ChargeQueryMapper;
import cn.wolfcode.crm.mapper.ClaimresultMapper;
import cn.wolfcode.crm.mapper.InsurancebillMapper;
import cn.wolfcode.crm.query.AccidentObjectQuery;
import cn.wolfcode.crm.query.PageResult;
import cn.wolfcode.crm.service.IAccidentService;
import cn.wolfcode.crm.util.CRMConstant;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author isriv
 * @date 2018/07/12 下午 08:55
 */

@Service
public class AccidentServiceImpl implements IAccidentService {

    @Autowired
    private AccidentMapper accidentMapper;
    @Autowired
    private ClaimresultMapper claimresultMapper;
    @Autowired
    private ChargeQueryMapper chargeQueryMapper;
    @Autowired
    private InsurancebillMapper insurancebillMapper;

    /**
     * 当前台穿过事故说明时,后台自动保存填该单的员工即理赔查勘员工currentUser,自动保存客户发起理赔时间claimTime
     * TODO 业务逻辑方面,暂时没有保存保险保单
     *
     * @param accident 传入的事故对象
     */
    @Override
    public void save(Accident accident) {
        accident.setClaimTime(new Date());
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        accident.setInvestigator(currentUser);
        accidentMapper.insert(accident);
    }

    @Override
    public void update(Accident accident) {
        Employee currentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        accident.setInvestigator(currentUser);
        accident.setClaimTime(new Date());
        accidentMapper.updateByPrimaryKey(accident);
    }

    @Override
    public Accident get(Long id) {
        return accidentMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Accident> getAll() {
        return accidentMapper.selectAll();
    }

    @Override
    public PageResult<Accident> query(AccidentObjectQuery qo) {
        int totalCount = accidentMapper.queryForCount(qo);
        if (totalCount == 0) {
            return new PageResult<>(totalCount, Collections.<Accident>emptyList());
        }
        return new PageResult<>(totalCount, accidentMapper.queryForList(qo));
    }

    /**
     * 对事故备案进行状态改变时,在查勘通过之后需要对备案记录审查人是谁,同时记录审查时间
     *
     * @param accident 传入封装进accident的值
     */
    @Override
    public void changeClaimState(Accident accident) {
        Employee curentUser = (Employee) SecurityUtils.getSubject().getPrincipal();
        Long claimId = accidentMapper.getClaimIdByAccidentId(accident.getId());
        // Long billId = chargeQueryMapper.getBillIdByClaimId(claimId);
        int claimState = accident.getClaimState();
        if (claimState >= CRMConstant.Accident.CLAIM_STATE_INVESTIGATE) {
            accident.setAuditor(curentUser);
            if (claimState == CRMConstant.Accident.CLAIM_STATE_AUDIT) {
                accident.setAuditorResult(CRMConstant.Accident.RESULT_AUDIT);
            } else if (claimState == CRMConstant.Accident.CLAIM_STATE_PAY) {
                Long billId = accidentMapper.selectByPrimaryKey(accident.getId()).getInsurancebill().getId();
                accident.setAuditorResult(CRMConstant.Accident.RESULT_PAY);
                claimresultMapper.updateClaimState(claimId, CRMConstant.Claimresult.PAY);
                insurancebillMapper.changState(billId, CRMConstant.Insurancebill.STATE_COMPENSATE);
            }
        }

        accident.setAuditorTime(new Date());
        accidentMapper.changeClaimState(accident);
    }

    public List<Map> queryCaseItem(AccidentObjectQuery qo) {
        return accidentMapper.queryCaseItem(qo);
    }
}

